package com.admin.propertistoreadmin;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;

import android.util.Log;

public class Streamer {
	private InputStream input;
	private HttpClient klien;
	private HttpGet req;
	private HttpPost post;
	private HttpResponse response;
	private BufferedReader dataInput;
	private HttpEntity httpEntity;
	private HttpParams params;

	public JSONArray getData(String url) {
		JSONArray jsonArray = null;
		try {
			input = null;
			klien = new DefaultHttpClient();
			params = klien.getParams();
			HttpConnectionParams.setTcpNoDelay(params, true);
			req = new HttpGet(url);
			response = klien.execute(req);
			input = response.getEntity().getContent();
			dataInput = new BufferedReader(new InputStreamReader(input), 8192);
			String baris;
			String isiWeb = "";
			StringBuilder sb = new StringBuilder();
			while ((baris = dataInput.readLine()) != null) {
				sb.append(baris + "\n");
			}
			isiWeb = sb.toString();
			jsonArray = new JSONArray(isiWeb);			
			dataInput.close();
			input.close();
		} catch (Exception e) {
			Log.e("gagal getData", e.getMessage());
		}
		return jsonArray;
	}
	public JSONArray postData(String url, List<NameValuePair> data) {
		JSONArray jsonArray = null;
		String h = null;
		try {
			klien = new DefaultHttpClient();
			params = klien.getParams();
			HttpConnectionParams.setTcpNoDelay(params, true);
			post = new HttpPost(url);
			post.setEntity(new UrlEncodedFormEntity(data));
			response = klien.execute(post);
			httpEntity = response.getEntity();
			input = httpEntity.getContent();
			dataInput = new BufferedReader(new InputStreamReader(input, "iso-8859-1"));
			StringBuilder sb = new StringBuilder();
			String baris = null;
			while ((baris = dataInput.readLine()) != null) {
				sb.append(baris + "\n");
			}
			dataInput.close();
			input.close();
			h = sb.toString();
			jsonArray = new JSONArray(sb.toString());
		
		} catch (Exception e) {
			Log.d("gagal postData", h);
			Log.e("gagal postData", e.getMessage());
			
		}
		return jsonArray;
	}
}
